package com.wyp.service.impl;

import com.wyp.mapper.ApartmentsMapper;
import com.wyp.mapper.QuestionsMapper;
import com.wyp.myutils.myutils;
import com.wyp.pojo.Apartments;
import com.wyp.pojo.Questions;
import com.wyp.service.QuestionsService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class QuestionsServiceImpl implements QuestionsService {

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private ApartmentsMapper apartmentsMapper;

    @Override
    public List<Map<String,Object>> qureyAllQuestions() {
        return questionsMapper.qureyAllQuestions();
    }

    @Override
    public List<Map<String,Object>> qureyQuestionsByMessage(String message,int apartmentid) {
        return questionsMapper.qureyQuestionsByMessage(message,apartmentid);
    }

    @Override
    public List<Map<String,Object>> qureyQuestionsByApartmentname(int  apartmentid) {
        return questionsMapper.qureyQuestionsByApartmentname(apartmentid);
    }

    @Override
    public Map<String,Object> qureyQuestionsByid(int questiontid) {
        return questionsMapper.qureyQuestionsByid(questiontid);
    }

    @Override
    public List<Map<String,Object>> qureyQuestionsByType(int type,int apartmentid) {
        return questionsMapper.qureyQuestionsByType(type,apartmentid);
    }

    @Override
    public boolean addQuestions(Questions questions) {
        return (questionsMapper.addQuestions(questions) != 0);
    }

    @Override
    public boolean updateQuestions(Questions questions) {
        return (questionsMapper.updateQuestions(questions) != 0);
    }

    //题目批量导入
    public int questionMultiImport(MultipartFile  file) throws Exception{
        int result = 0;
//		存放excel表中所有记录
        List<Questions> questionsList = new ArrayList<>();
        /**
         *
         * 判断文件版本
         */
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
        InputStream ins = file.getInputStream();
        Workbook wb = null;
        if(suffix.equals("xlsx")){
            wb = new XSSFWorkbook(ins);
        }else{
            wb = new HSSFWorkbook(ins);
        }
        /**
         * 获取excel表单
         */
        Sheet sheet = wb.getSheetAt(0);
        /**
         * line = 2 :从表的第三行开始获取记录
         *
         */
        if(null != sheet){
            for(int line = 1; line <= sheet.getLastRowNum();line++){
                Questions question = new Questions();
                Row row = sheet.getRow(line);
                if(null == row){
                    continue;
                }
                /**
                 * 判断单元格类型是否为文本类型
                 */
                if(CellType.STRING != row.getCell(0).getCellType()){
                    throw new Exception("单元格类型不是文本类型！");
                }
                /**
                 * 获取单元格的内容
                 */
                List<Apartments> apartments = apartmentsMapper.qureyAllApartment();
                for (Apartments apartment : apartments) {
                    if (row.getCell(0).getStringCellValue().trim().equals(apartment.getApartmentname())) {
                        question.setApartmentid(apartment.getApartmentid());
                        break;
                    }
                }
                if (row.getCell(1).getStringCellValue().trim().equals("单选")) {
                    question.setType(1);
                }
                if (row.getCell(1).getStringCellValue().trim().equals("多选")) {
                    question.setType(2);
                }
                if (row.getCell(1).getStringCellValue().trim().equals("判断")) {
                    question.setType(3);
                }
                question.setContent(row.getCell(2).getStringCellValue().trim());
                if (row.getCell(3) != null) {
                    question.setOptionA(row.getCell(3).getStringCellValue().trim());
                }
                else {
                    question.setOptionA("");
                }
                if (row.getCell(4) != null) {
                    question.setOptionB(row.getCell(4).getStringCellValue().trim());
                }
                else {
                    question.setOptionB("");
                }
                if (row.getCell(5) != null) {
                    question.setOptionC(row.getCell(5).getStringCellValue().trim());
                }
                else {
                    question.setOptionC("");
                }
                if (row.getCell(6) != null) {
                    question.setOptionD(row.getCell(6).getStringCellValue().trim());
                }
                else {
                    question.setOptionD("");
                }
                if (row.getCell(7) != null) {
                    question.setOptionE(row.getCell(7).getStringCellValue().trim());
                }
                else {
                    question.setOptionE("");
                }
                if (row.getCell(8) != null) {
                    question.setOptionF(row.getCell(8).getStringCellValue().trim());
                }
                else {
                    question.setOptionF("");
                }
                if (row.getCell(9) != null) {
                    if (row.getCell(9).getStringCellValue().trim().equals("正确")) {
                        question.setJudgeanswer(true);
                    }
                    if (row.getCell(9).getStringCellValue().trim().equals("错误")) {
                        question.setJudgeanswer(false);
                    }
                }
                if (row.getCell(10) != null) {
                    question.setAnswer(new myutils().delspace(row.getCell(10).getStringCellValue().trim()));
                }
                question.setPoint((int)row.getCell(11).getNumericCellValue());
                questionsList.add(question);
            }
            for(Questions questions:questionsList){
                /**
                 * 逐行添加进数据库
                 */
                result = questionsMapper.addQuestions(questions);
            }
        }
        return result;
    }
}
